/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package Wires.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMBoolean;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMNumber;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMOclAny;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMString;
import org.eclipse.m2m.atl.ocl.core.OclQuery;

import Wires.ConnectableElement;
import Wires.DecisionNode;
import Wires.InputActualParameter;
import Wires.Transformation;
import Wires.WiresPackage;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Decision Node</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link Wires.impl.DecisionNodeImpl#getTrueBranch <em>True Branch</em>}</li>
 *   <li>{@link Wires.impl.DecisionNodeImpl#getFalseBranch <em>False Branch</em>}</li>
 *   <li>{@link Wires.impl.DecisionNodeImpl#getExpression <em>Expression</em>}</li>
 *   <li>{@link Wires.impl.DecisionNodeImpl#getInParams <em>In Params</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class DecisionNodeImpl extends WiresElementImpl implements DecisionNode {
	
	/***************************************************/
	/***************** Modifications *******************/
	
	public void execute(ConnectableElement incoming) throws Exception{		
		
		List<InputActualParameter> inparam = this.getInParams();
		//Check if the input parameters are ready
		boolean ready = false;
		for (InputActualParameter parameter : inparam){
			ready = ready || ((InputActualParameterImpl)parameter).getActive();
		}				
		
		if (ready){		
			String expression = this.getExpression();
			//Replace the variables with their actual values
			for (InputActualParameter parameter : inparam){
				String paramName = parameter.getName();
				ASMOclAny value = ((InputActualParameterImpl)parameter).getBasicDataValue();
				if (value instanceof ASMString){
					expression = expression.replaceAll(paramName, "'"+value.toString()+"'");
				}else if (value instanceof ASMBoolean || value instanceof ASMNumber){
					expression = expression.replaceAll(paramName, value.toString());
				}
			}
			OclQuery query = new OclQuery(expression);						
			
			//Evaluate the expression
			boolean decision = query.evalBoolean(Collections.EMPTY_MAP);
			//Take the decision
			for (Transformation t : this.getTrueBranch()){
				((TransformationImpl)t).setActive(decision);
				if (decision)
					((TransformationImpl)t).execute(null);
			}
			
			for (Transformation t : this.getFalseBranch()){
				((TransformationImpl)t).setActive(!decision);
				if (!decision)
					((TransformationImpl)t).execute(null);				
			}					
		}
		
	}
	
	/**************** Modifications End ****************/
	/***************************************************/
	
	/**
	 * The cached value of the '{@link #getTrueBranch() <em>True Branch</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTrueBranch()
	 * @generated
	 * @ordered
	 */
	protected EList<Transformation> trueBranch;

	/**
	 * The cached value of the '{@link #getFalseBranch() <em>False Branch</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFalseBranch()
	 * @generated
	 * @ordered
	 */
	protected EList<Transformation> falseBranch;

	/**
	 * The default value of the '{@link #getExpression() <em>Expression</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getExpression()
	 * @generated
	 * @ordered
	 */
	protected static final String EXPRESSION_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getExpression() <em>Expression</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getExpression()
	 * @generated
	 * @ordered
	 */
	protected String expression = EXPRESSION_EDEFAULT;

	/**
	 * The cached value of the '{@link #getInParams() <em>In Params</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getInParams()
	 * @generated
	 * @ordered
	 */
	protected EList<InputActualParameter> inParams;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected DecisionNodeImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return WiresPackage.Literals.DECISION_NODE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Transformation> getTrueBranch() {
		if (trueBranch == null) {
			trueBranch = new EObjectResolvingEList<Transformation>(Transformation.class, this, WiresPackage.DECISION_NODE__TRUE_BRANCH);
		}
		return trueBranch;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Transformation> getFalseBranch() {
		if (falseBranch == null) {
			falseBranch = new EObjectResolvingEList<Transformation>(Transformation.class, this, WiresPackage.DECISION_NODE__FALSE_BRANCH);
		}
		return falseBranch;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getExpression() {
		return expression;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setExpression(String newExpression) {
		String oldExpression = expression;
		expression = newExpression;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, WiresPackage.DECISION_NODE__EXPRESSION, oldExpression, expression));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InputActualParameter> getInParams() {
		if (inParams == null) {
			inParams = new EObjectContainmentEList<InputActualParameter>(InputActualParameter.class, this, WiresPackage.DECISION_NODE__IN_PARAMS);
		}
		return inParams;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case WiresPackage.DECISION_NODE__IN_PARAMS:
				return ((InternalEList<?>)getInParams()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case WiresPackage.DECISION_NODE__TRUE_BRANCH:
				return getTrueBranch();
			case WiresPackage.DECISION_NODE__FALSE_BRANCH:
				return getFalseBranch();
			case WiresPackage.DECISION_NODE__EXPRESSION:
				return getExpression();
			case WiresPackage.DECISION_NODE__IN_PARAMS:
				return getInParams();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case WiresPackage.DECISION_NODE__TRUE_BRANCH:
				getTrueBranch().clear();
				getTrueBranch().addAll((Collection<? extends Transformation>)newValue);
				return;
			case WiresPackage.DECISION_NODE__FALSE_BRANCH:
				getFalseBranch().clear();
				getFalseBranch().addAll((Collection<? extends Transformation>)newValue);
				return;
			case WiresPackage.DECISION_NODE__EXPRESSION:
				setExpression((String)newValue);
				return;
			case WiresPackage.DECISION_NODE__IN_PARAMS:
				getInParams().clear();
				getInParams().addAll((Collection<? extends InputActualParameter>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case WiresPackage.DECISION_NODE__TRUE_BRANCH:
				getTrueBranch().clear();
				return;
			case WiresPackage.DECISION_NODE__FALSE_BRANCH:
				getFalseBranch().clear();
				return;
			case WiresPackage.DECISION_NODE__EXPRESSION:
				setExpression(EXPRESSION_EDEFAULT);
				return;
			case WiresPackage.DECISION_NODE__IN_PARAMS:
				getInParams().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case WiresPackage.DECISION_NODE__TRUE_BRANCH:
				return trueBranch != null && !trueBranch.isEmpty();
			case WiresPackage.DECISION_NODE__FALSE_BRANCH:
				return falseBranch != null && !falseBranch.isEmpty();
			case WiresPackage.DECISION_NODE__EXPRESSION:
				return EXPRESSION_EDEFAULT == null ? expression != null : !EXPRESSION_EDEFAULT.equals(expression);
			case WiresPackage.DECISION_NODE__IN_PARAMS:
				return inParams != null && !inParams.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (expression: ");
		result.append(expression);
		result.append(')');
		return result.toString();
	}

} //DecisionNodeImpl
